package com.bbx.demo.grpc.server.impl;

import com.bbx.demo.grpc.generated.*;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;

/**
 * 服务端接口实现方法
 */
@Slf4j
public class RouteService extends RouteGuideGrpc.RouteGuideImplBase {

    /**
     * 简单的请求 响应
     *
     * @param request
     * @param responseObserver
     */
    @Override
    public void getFeature(Point request, StreamObserver<Feature> responseObserver) {
        log.info("简单的请求 响应~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        log.info("简单的请求 响应, request.getLatitude[{}]", request.getLatitude());
        log.info("简单的请求 响应, request.getLongitude[{}]", request.getLongitude());
        responseObserver.onNext(Feature.newBuilder()
                .setName("张三")
                .setLocation(Point.newBuilder()
                        .setLatitude(12345)
                        .setLongitude(67890)
                        .build())
                .build());
        responseObserver.onCompleted();
    }

    /**
     * 简单请求  流式响应
     *
     * @param request
     * @param responseObserver
     */
    @Override
    public void listFeatures(Rectangle request, StreamObserver<Feature> responseObserver) {
        log.info("简单请求  流式响应~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        log.info("Lo_Longitude[{}],Lo_Latitude[{}]", request.getLo().getLongitude(), request.getLo().getLatitude());
        log.info("Hi_Longitude[{}],Hi_Latitude[{}]", request.getHi().getLongitude(), request.getHi().getLatitude());
        for (int i = 0 ;i<4;i++) {
            responseObserver.onNext(Feature.newBuilder().setName("i"+i).setLocation(Point.newBuilder().setLongitude(i).setLatitude(i).build()).build());
        }
       responseObserver.onCompleted();
    }

    /**
     * 流式请求 简单响应
     *
     * @param responseObserver
     * @return
     */
    @Override
    public StreamObserver<Point> recordRoute(StreamObserver<RouteSummary> responseObserver) {
        log.info("流式请求 简单响应~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        return new StreamObserver<Point>() {
            @Override
            public void onNext(Point value) {
                log.info("Longitude[{}],Latitude[{}]", value.getLongitude(), value.getLatitude());
            }

            @Override
            public void onError(Throwable t) {
                log.error("It has a error:", t);
            }

            @Override
            public void onCompleted() {
                responseObserver.onNext(RouteSummary.newBuilder().setDistance(1).setElapsedTime(2).setFeatureCount(3).setPointCount(4).build());
                responseObserver.onCompleted();
            }
        };
    }

    /**
     * 流式请求、流式响应
     *
     * @param responseObserver
     * @return
     */
    @Override
    public StreamObserver<RouteNote> routeChat(StreamObserver<RouteNote> responseObserver) {
        log.info("流式请求、流式响应~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        return new StreamObserver<RouteNote>() {
            @Override
            public void onNext(RouteNote value) {
                log.info("Message[{}],Location[{}],Latitude[{}]", value.getMessage(), value.getLocation().getLatitude(), value.getLocation().getLatitude());
                responseObserver.onNext(RouteNote.newBuilder().setMessage("流式响应").setLocation(Point.newBuilder().setLatitude(123).setLongitude(456).build()).build());
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onCompleted() {
                responseObserver.onCompleted();
            }
        };
    }
}
